//
//  UIBarButtonItem+category.m
//  ESport
//
//  Created by mac  on 14-11-17.
//  Copyright (c) 2014年 kdong. All rights reserved.
//

#import "UIBarButtonItem+category.h"

@implementation UIBarButtonItem (category)

+ (UIBarButtonItem *)barButtonItemWithImageName:(NSString *)imageName target:(id)target sel:(SEL)sel
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.adjustsImageWhenHighlighted = NO;
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
     [button setExclusiveTouch:YES];/*禁用按钮同时点击*/
    button.height = button.currentImage.size.height;
    button.width = button.currentImage.size.width;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}

+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title target:(id)target sel:(SEL)sel titleFont:(UIFont *)titleFont titleColor:(UIColor *)titleColor

{
    CGSize titleSize = [title sizeWithFont:titleFont maxSize:CGSizeMake(300, MAXFLOAT)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.adjustsImageWhenHighlighted = NO;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:RGBACOLORWithAlpha(255, 255, 255, 0.6f) forState:UIControlStateDisabled];
    button.titleLabel.font = titleFont;
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    [button setExclusiveTouch:YES];/*禁用按钮同时点击*/
    button.size = titleSize;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}

+(UIBarButtonItem *)BarButtonItemWithImageName:(NSString *)imageName title:(NSString *)title target:(id)target sel:(SEL)sel titleFont:(UIFont *)titleFont titleColor:(UIColor *)titleColor
{
    if (imageName.length <= 0 && title.length > 0) {
       return [self barButtonItemWithTitle:title target:target sel:sel titleFont:titleFont titleColor:titleColor];
    } else if (imageName.length > 0 && title.length <= 0){
      return [self barButtonItemWithImageName:imageName target:target sel:sel];
    } else if (imageName.length > 0 && title.length > 0){
        
        CGSize titleSize = [title sizeWithFont:titleFont maxSize:CGSizeMake(300, MAXFLOAT)];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        //    button.backgroundColor = [UIColor redColor];
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:titleColor forState:UIControlStateNormal];
        button.titleLabel.font = titleFont;
        [button setExclusiveTouch:YES];/*禁用按钮同时点击*/
        [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
        button.height = titleSize.height > button.currentImage.size.height ? titleSize.height : button.currentImage.size.height;
        button.width = titleSize.width + button.currentImage.size.width + 3;
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
        item.title = title;
        return item;
    } else {
        return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:target action:sel];
    }
}

+ (UIBarButtonItem *)BarButtonItemWithImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName target:(id)target sel:(SEL)sel
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.adjustsImageWhenHighlighted = NO;
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    [button setExclusiveTouch:YES];/*禁用按钮同时点击*/
    button.height = button.currentImage.size.height;
    button.width = button.currentImage.size.width;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;

}

- (void)setSelected:(BOOL)selected
{
    UIButton *button = (UIButton *)self.customView;
    button.selected = selected;
}

- (BOOL)selected
{
     UIButton *button = (UIButton *)self.customView;
    return button.selected;
}
@end
